import React, { useState } from "react";
import { Spin, Empty, message } from "antd";
import { ControlType as TYPE } from '@components'
import { session } from "@core";

import { getAddressFindRegion } from "@apis/markAreaApi";

const untiOptions = session.get("unitData") ? session.get("unitData") : [];
const riskOptions = session.get("riskData") ? session.get("riskData") : [];

const useGetFields = (formRef) => {
    const [loading, setLoading] = useState(false);
    const [provOptions, setProvOptions] = useState([]);
    const [cityOptions, setCityOptions] = useState([]);
    const [countyOptions, setCountyOptions] = useState([]);

    const getProvince = async () => {
        if (provOptions.length) return;
    
        const params = {
          type: "province",
          code: "",
        }
        setLoading(true)
        const {data, error} = await getAddressFindRegion(params)
        setLoading(false)
        if (error) return
    
        let list = [];
        for (const i of data) {
          const { provinceCode: value, provinceName: label } = i;
          list.push({ value, label });
        }
        setProvOptions(list);
    
    };

    const getCity = async () => {
        const code = formRef.current.getFieldValue("province")?.value;
        if (!code) return message.info("提示：请先选择省份") 
    
        const params = {
          type: "city",
          code,
        }
        setLoading(true)
        const {data, error} = await getAddressFindRegion(params)
        setLoading(false)
        if (error) return
    
        let list = [];
        for (const i of data) {
          const { cityCode: value, cityName: label } = i;
          list.push({ value, label });
        }
        setCityOptions(list);
    
    };

    const getCounty = async () => {
        const code = formRef.current.getFieldValue("city")?.value
        if (!code) return message.info("提示：请先选择城市")
    
        const params = {
            type: "county",
            code,
        }
        setLoading(true)
        const {data, error} = await getAddressFindRegion(params)
        setLoading(false)
        if (error) return
    
        let list = [];
        for (const i of data) {
          const { countyCode: value, countyName: label } = i;
          list.push({ value, label });
        }
        setCountyOptions(list);
    
    };

    return [
        { 
            name: 'province', label: "省份", controlType: TYPE.SELECT, showSearch: true, labelInValue: true,
            optionFilterProp: "children",
            filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
            notFoundContent: loading ? <Spin size="small" /> : null,
            options: provOptions,
            onFocus: () => getProvince(),
        },
        { 
            name: 'city', label: '城市', controlType: TYPE.SELECT, showSearch: true, labelInValue: true,
            optionFilterProp: "children",
            filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
            notFoundContent: loading ? <Spin size="small" /> : null,
            options: cityOptions,
            onFocus: () => getCity()
        },
        { 
            name: 'county', label: '县区', controlType: TYPE.SELECT, showSearch: true, labelInValue: true,
            optionFilterProp: "children",
            filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
            notFoundContent: loading ? <Spin size="small" /> : null,
            options: countyOptions,
            onFocus: () => getCounty()
        },
        { 
            name: 'unti', label: '机构', controlType: TYPE.SELECT, showSearch: true,
            optionFilterProp: "children",
            filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
            options: untiOptions,
        },
        { 
            name: 'risk', label: '险种', controlType: TYPE.SELECT, showSearch: true,
            optionFilterProp: "children",
            filterOption: (input, option) => option.children.toLowerCase()?.includes(input.toLowerCase()),
            options: riskOptions,
        },
        { name: 'insured', label: '被保险人', controlType: TYPE.INPUT },
        { name: 'policyNo', label: '保单号', controlType: TYPE.INPUT },
    ]
}

export default useGetFields